<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class ventas_model extends CI_Model {


	function __construct()
	{
		parent::__construct();
	}
	
	public function get_ventas($paginados = true)
	{
		$sql = '';
		//paginacion
		if($paginados)
		{
			$this->load->library('pagination');
			$params = array(
					'result_items_per_page' => '50',
					'result_page' => (isset($_GET['pag'])? $_GET['pag']: 0)
			);
			if($params['result_page'] % $params['result_items_per_page'] == 0)
				$params['result_page'] = ($params['result_page']/$params['result_items_per_page']);
		}
		//Filtros para buscar
		if($this->input->get('ffolio') != '')
			$sql .= " AND p.id_paquete = ".$this->input->get('ffolio');
		
		if($this->input->get('fnombre') != '')
			$sql .= " AND lower(u.nombre) LIKE '%".mb_strtolower($this->input->get('fnombre'), 'UTF-8')."%'";
		
		if($this->input->get('fstatus') != '' && $this->input->get('fstatus') != 'todos')
		{
			$sql .= " AND p.status = ".$this->input->get('fstatus');
		}
		
		if ($this->input->get('ftipo') != '' && $this->input->get('ftipo') != 'todos')
		{
			$sql .= " AND p.tipo = '".$this->input->get('ftipo')."'";
		}
		
		if ($this->input->get('ffecha_ini') != '') 
		{
			$sql .= " AND DATE(p.fecha_creacion) >='".$this->input->get('ffecha_ini')."'".(($this->input->get('ffecha_fin')!='')?" AND DATE(p.fecha_creacion) <= '".$this->input->get('ffecha_fin')."'":"");
		}

		$query = BDUtil::pagination("
				SELECT p.id_paquete AS folio, u.id_usuario, u.nombre, Date(p.fecha_creacion) AS fecha_creacion, 
					Date(p.fecha_vencimiento) AS fecha_vencimiento, p.costo, p.forma_pago, 
					IF(p.tipo='todo', 'ilimitado', 'individual') AS tipo, p.meses, p.status
				FROM paquetes AS p 
					INNER JOIN usuarios AS u ON p.id_usuario = u.id_usuario 
				WHERE u.status = 1 ".$sql." 
				ORDER BY p.id_paquete ASC
				", $params, true);
		$res = $this->db->query($query['query']);
		
		$total_ventas = 0;
		foreach ($query['resultset']->result() as $key => $value) {
			$total_ventas += $value->costo;
		}
			
		$response = array(
				'ventas'         => array(),
				'total_ventas'   => $total_ventas,
				'total_rows'     => $query['total_rows'],
				'items_per_page' => $params['result_items_per_page'],
				'result_page'    => $params['result_page']
		);
		if($res->num_rows() > 0)
			$response['ventas'] = $res->result();
		
		return $response;
	}

	
}
/* End of file usuarios_model.php */
/* Location: ./application/controllers/usuarios_model.php */